package com.cqlfn.business.dataCoordinate.webupload.action;

import com.cqlfn.business.common.constants.CqlfnConstant;
import com.cqlfn.business.data.datapairingfill.service.DataPairingFillService;
import com.cqlfn.business.dataCoordinate.webupload.annotation.OperatManagementSwagger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ClassName DataPairingFillController
 * @Description 数据对码填报管理模块
 * @Author yuanfeng
 * @Date 2019/7/24 10:37
 * @Version 1.0
 **/
@OperatManagementSwagger
@Api(description = "数据对码填报管理")
@RestController
@RequestMapping(CqlfnConstant.UPLOAD_PATH + "/dataPairingFill")
@Validated
public class DataPairingFillController {

    /**
     * 日志打印对象
     **/
    private static final Logger LOGGER = LoggerFactory.getLogger(DataPairingFillController.class);

    /**
     * 注入数据对码上传填报Service
     */
    @Autowired
    private DataPairingFillService dataPairingFillService;


    /**
     * @return 返回数据库中所有的表
     * @Description: 获取所有的表
     * @Param [] null
     * @date: 2019/8/10 22:12
     */
    @GetMapping("/getAllTables")
    @ApiOperation(value = "得到所有表")
    public ResponseEntity<Map<String, Object>> getAllTables() {

        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> list = dataPairingFillService.getAllTables();
            resultMap.put("data", list);
            resultMap.put("code", 200);
            resultMap.put("message", "响应成功");
        } catch (Exception e) {
            LOGGER.error("" + e);
            resultMap.put("code", 500);
            resultMap.put("message", "未获得所有表");
        }
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }


    /**
     * @return 返回字段的规则
     * @Description: 前端字段校验接口
     * @Param tableName 表名
     * @date: 2019/8/10 22:11
     */
    @GetMapping("/validation")
    @ApiOperation(value = "前端字段校验接口")
    public ResponseEntity<Map<String, Object>> validationColumn(@ApiParam(value = "表名")@RequestParam(value = "tableName", required = true) String tableName) {
        Map<String, Object> resultMap = new HashMap<>();

        try {
            List<Map<String, Object>> columnList  = dataPairingFillService.validationColumn(tableName);
            resultMap.put("data",columnList);
            resultMap.put("code", 200);
            resultMap.put("message", "响应成功");
        } catch (Exception e) {
            LOGGER.error("" + e);
            resultMap.put("data",null);
            resultMap.put("code", 402);
            resultMap.put("message", "服务器繁忙");
        }
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

    /**
     * @return 新增出现的失败异常状态返回
     * @Description: 增加元素到前置机和中央仓数据库中
     * @Param [map] 插入的map数据
     * @date: 2019/8/5 13:56
     */
    @PostMapping("/addData")
    @ApiOperation(value = "新增数据接口")
    public ResponseEntity<Map<String, Object>> addData(@ApiParam(value = "前端封装的插入数据，其中包括表名")@RequestBody Map<String, Object> map) {

        System.out.println("新增的map数据"+map);
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> addDataMap = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (Objects.isNull(map)) {
                addDataMap.put("message", "新增数据为空");
                addDataMap.put("code", 403);
                addDataMap.put("data",resultList);
                return new ResponseEntity<>(addDataMap, HttpStatus.OK);
            }
            //调用新增数据业务逻辑
            resultList = dataPairingFillService.insertToMysqlAndWareHouse(map, resultList, resultMap,(String) map.get("tableName"));

            for(int i = 0;i < resultList.size();i++){

                if(resultList.get(i).containsKey("state")){

                if(1 == (Integer) resultList.get(i).get("state")){
                    addDataMap.put("code", 200);
                    addDataMap.put("data", resultList);
                    addDataMap.put("message", "响应成功");
                }else {
                    addDataMap.put("code", 500);
                    addDataMap.put("data", resultList);
                    addDataMap.put("message", "数据验证失败");
                }
            }
            }
        } catch (Exception e) {
            LOGGER.error("" + e);
            addDataMap.put("code", 500);
            addDataMap.put("data", resultList);
            addDataMap.put("message", "新增数据失败");
        }
        return new ResponseEntity<>(addDataMap, HttpStatus.OK);
    }

    /**
     * @return 查询到的所有分页数据
     * @Description:根据用户选择的表查询并分页
     * @Param tableName  表名
     * @Param pageable  分页对象
     * @date: 2019/8/3 22:33
     */
    @GetMapping("/queryByTableName")
    @ApiOperation(value = "查询分页接口")
    public ResponseEntity<Map<String, Object>> queryByTableName(@ApiParam(value = "表名") @RequestParam(value = "tableName", required = true) String tableName, @ApiParam(value = "分页对象") Pageable pageable) {

        Map<String, Object> resultMap = new HashMap<>();
        try {

            if (StringUtils.isBlank(tableName)) {
                resultMap.put("message", "表名为空");
                resultMap.put("code", 500);
                resultMap.put("data", null);
            }
            resultMap = dataPairingFillService.listByTableName(pageable, tableName, resultMap);
        } catch (Exception e) {
            LOGGER.error("" + e);
            resultMap.put("code", 402);
            resultMap.put("message", "服务器繁忙");
        }
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

    /**
     * @return 返回选定表的所有列
     * @Description:获取表的列名返回给前端
     * @Param [tableName]    表名
     * @date: 2019/8/3 22:33
     */
    @ApiOperation(value = "获取表的列名")
    @GetMapping("/getColumnName")
    public ResponseEntity<Map<String, Object>> getColumnName(@ApiParam(value = "表名") @RequestParam(value = "tableName", required = true) String tableName) {

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> paramMap = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        try {
            list = dataPairingFillService.getColumnName(tableName, list, paramMap);
            resultMap.put("data", list);
            resultMap.put("code", 200);
        } catch (Exception e) {
            LOGGER.error("" + e);
            resultMap.put("data", null);
            resultMap.put("code", 500);
        }
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

    /**
     * @return 返回所有该详情的字段
     * @Description:点击详情后显示该条详情的字段
     * @Param [columnId] 列ID
     * @Param tableName 表名
     * @date: 2019/8/3 22:33
     */

    @GetMapping("/showDetail")
    @ApiOperation(value = "点击详情后显示字段属性")
    public ResponseEntity<Map<String, Object>> showDetail(@ApiParam(value = "表名") @RequestParam(value = "tableName", required = true) String tableName, @ApiParam(value = "列属性的id") @RequestParam Long columnId) {

        Map<String, Object> paramMap = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> listResult = null;

        try {
            if (Objects.isNull(columnId)) {
                resultMap.put("code", 500);
                resultMap.put("message", "列ID为空");
            }
            listResult = dataPairingFillService.listDetail(paramMap, columnId, tableName);
            resultMap.put("code", 200);
        } catch (Exception e) {
            LOGGER.error("" + e);
            resultMap.put("code", 500);
        }
        //设置到data中
        resultMap.put("data", listResult);
        //响应并返回封装的数据
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }


}
